package cn.helios.controller;

import cn.helios.domain.User;
import cn.helios.feign.exceptionHandler;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RefreshScope
public class UserController {
    @Value("${test}")
    private Long test;

    @GetMapping("/user/{id}")
    @SentinelResource(  value="user",
                        blockHandler="exceptionHandler",
                        blockHandlerClass = exceptionHandler.class,
                        fallback = "getByIdfallback")
    public User getById(@PathVariable Long id){
        int i = 1 / 0;
        return User.builder().id(test).username("儿子").desc("嗨嗨！").build();
    }

//    public User exceptionHandler(@PathVariable Long id, BlockException ex) {
//        ex.printStackTrace();
//        System.out.println("限流了...");
//        return new User(-1L,"限流了","限流了");
//    }

    public User getByIdfallback(@PathVariable Long id){
        return new User(id,"zs:"+id, "熔断托底了");
    }
}
